package com.wutao.panzh.index.content.controller;

;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.RouteClassification;
import com.panzh.entity.RouteImag;
import com.panzh.entity.RouteItem;
import com.panzh.entity.index.IndexRouteItem;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.ProjectParames;
import com.panzh.myconst.UserContst;
import com.wutao.panzh.service.RouteClassificationService;
import com.wutao.panzh.service.RouteItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexRouteItemHandler {

    @Autowired
    private ObjectMapper objectMapper;

    @Reference
    private RouteClassificationService routeClassificationService;

    @Reference
    private RouteItemService routeItemService;


    @ResponseBody
    @RequestMapping("/get/route/classification")
    public ResultVO getSceneryClassification() {
        List<RouteClassification> allClassIfication = null;
        try {
            allClassIfication = routeClassificationService.getAllClassIfication();
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.GET_INFORMATION_FAILED, ResultVO.NO_DATA);
        }
        return new ResultVO<List<RouteClassification>>(ResultVO.SUCCESS, ResultVO.NO_MSG, allClassIfication);
    }

    /**
     * 咱们默认还是要使用一个分页的东东的。classificationId是分类的id，更加方便我们进行属性的筛选；
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/get/all/index/route")
    public ResultVO getAllIndexFoodItem(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "classificationId", required = false, defaultValue = "0") Integer classificationId) {
        PageInfo<RouteItem> indexRouteItemPageInfo = routeItemService.getAllIndexRouteItem(pageNo, ProjectParames.PAGE_SIZE, classificationId);
        return new ResultVO<PageInfo<RouteItem>>(ResultVO.SUCCESS, ResultVO.NO_MSG, indexRouteItemPageInfo);
    }

    //    href='show_scenery_details?itemId=" + itemId + "'
    @RequestMapping("show_route_details")
    public String getShowIndexFoodDetails(@RequestParam("itemId") Integer itemId, Model model) throws JsonProcessingException {
        //转来转去，我是真的吧自己弄烦了。
        RouteItem routeItem = routeItemService.getRouteItemById(String.valueOf(itemId));
        List<RouteImag> imags = new ArrayList<>();
        //如果不经过处理，这样返回前端估计就是一个字符串了；
        for (RouteImag routeImag : routeItem.getRouteImags()) {
            imags.add(routeImag);
        }
        String imagesJSON = objectMapper.writeValueAsString(imags);
        model.addAttribute("imagesJSON", imagesJSON);
        //这里我们写的比较冗余，为了在前端用一下thymeleaf的循环操作；
        model.addAttribute("routeItem", routeItem);
        return "routeDetails";
    }

    //    $.get("add/collection",{"itemId":itemId,"userId":userId}
    @ResponseBody
    @GetMapping("/add/collection")
    public ResultVO addCollection(@RequestParam("itemId") String itemId, @RequestParam("userId") String userId) throws JsonProcessingException {
        try {
            routeItemService.addCollection(Integer.parseInt(itemId), Integer.parseInt(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DATA_SAVING_FAILED, ResultVO.NO_MSG);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY, ResultVO.NO_MSG);
    }
}
